﻿using Datav.Design.Datas.Pocos;
using XCode;
using Zdap.Common;
using Zdap.Common.IdGenerate;
using Zdap.XCode.Service;

namespace Datav.Design.IService
{
    /// <summary>
    ///
    /// </summary>
    public class SubjectAbstractService : BaseService<SubjectAbstract, SubjectAbstractModel>, ISubjectAbstractService
    {
        /// <summary>
        ///
        /// </summary>
        /// <param name="ProjectId"></param>
        /// <param name="models"></param>
        /// <returns></returns>
        public async ValueTask<ResponseContext<bool>> ImportSubjectAbstract(long ProjectId, List<ImprotSubjectAbstractModel> models)
        {
            var subjects = await SubjectAbstract.FindAllAsync(SubjectAbstract._.ProjectId == ProjectId & SubjectAbstract._.IsDelete == 0);
            List<SubjectAbstract> instSubjects = new List<SubjectAbstract>();
            foreach (var item in models)
            {
                if (subjects.Any(x => x.SumaryName == item.Name))
                    continue;
                if (!string.IsNullOrWhiteSpace(item.Name))
                {
                    SubjectAbstract zdapSubject = subjects.Find(x => x.SumaryName == item.Name);
                    if (zdapSubject == null)
                        continue;
                    if (subjects.Any(x => x.SumaryName == $"{zdapSubject.SumaryName}-{item.Name}"))
                        continue;
                    SubjectAbstract subject = new SubjectAbstract();
                    subject.Id = IdWorker.NextId();
                    subject.SumaryName = $"{item.Name}";
                    subject.ProjectId = ProjectId;
                    // CommonService.SetUpdateProperty(e, currentUser);
                    subject.CreateTime = DateTime.Now;
                    subject.UpdateTime = DateTime.Now;
                    instSubjects.Add(subject);
                    subjects.Add(subject);
                }
                else
                {
                    SubjectAbstract subject = new SubjectAbstract();
                    subject.Id = IdWorker.NextId();
                    subject.SumaryName = item.Name;
                    subject.ProjectId = ProjectId;
                    subject.CreateTime = DateTime.Now;
                    subject.UpdateTime = DateTime.Now;
                    instSubjects.Add(subject);
                    subjects.Add(subject);
                }
            }
            instSubjects.BatchInsert();
            return new ResponseContext<bool> { Data = true };
        }
    }
}